package com.liu.netty3.server;

import com.liu.netty3.handler.MsgHandler;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2019/8/28 0028.
 */
public class Server {

    public static void main(String[] args) {
//服务类
        ServerBootstrap bootstrap = new ServerBootstrap();

        //boss线程监听端口，worker线程负责数据读写
        ExecutorService boss = Executors.newCachedThreadPool();
        ExecutorService worker = Executors.newCachedThreadPool();

        //设置niosocket工厂
          bootstrap.setFactory(new NioServerSocketChannelFactory(boss,worker));
//        设置管道工厂
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
//                注意这里是为了前台测试的时候，我们使用的是GBK 的格式发送，所以要讲解码器 也设置成GBK
//                为了能够是的客户端，不乱吗，所以要将编码器也设置成GBK格式
                pipeline.addLast("decoder",new StringDecoder(Charset.forName("GBK")));
                pipeline.addLast("encoder",new StringEncoder(Charset.forName("GBK")));
                pipeline.addLast("MsgHandler",new MsgHandler());
                return pipeline;
            }
        });
        bootstrap.bind(new InetSocketAddress(10030));
        System.out.println("服务端已经启动！！！");
    }
}
